
'''本文件使用例子介绍wx中文本的用法.'''


import wx
import wx.lib.buttons as buttons


class TextFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Test Text', size=(400, 300))
        self.panel = wx.Panel(self, -1)
        self.testButton()

    def testButton(self):
        '''测试按钮控件'''
        #文本按钮
        btn = wx.Button(self.panel, -1, "Hello", pos=(50, 20))
        self.Bind(wx.EVT_BUTTON, self.sayhello, btn)
        btn.SetDefault()

        #位图按钮
        img = wx.Image('../入门/ac-camera@3x.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        btn2 = wx.BitmapButton(self.panel, -1, img, pos=(50, 50), style=0)
        self.Bind(wx.EVT_BUTTON, self.cut, btn2)

        # 开关按钮
        self.toggleBtn = wx.ToggleButton(self.panel, -1, 'Touch to on', pos=(50, 140))
        self.Bind(wx.EVT_TOGGLEBUTTON, self.toggleEvent, self.toggleBtn)

        #通用按钮
        sizer = wx.FlexGridSizer(1, 3, 20, 20)
        b = buttons.ThemedGenButton(self.panel, -1, 'Gen Button')
        b.Enable(True)
        b.SetBackgroundColour('red')
        b.SetForegroundColour('white')
        b.SetLabel('Gen Btn')
        b.SetBezelWidth(5)
        b.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD, False))
        b.SetToolTipString('This is a Big button')
        sizer.Add(b)




    def sayhello(self, event):
        print('Say hello')

    def cut(self, event):
        print('cut')

    def toggleEvent(self, event):
        if self.toggleBtn.GetValue():
            self.toggleBtn.SetLabel('Touch me off')
        else:
            self.toggleBtn.SetLabel('Touch me on')

    def testText(self):
        '''测试文本'''
        # 这是一个基本的静态文本
        wx.StaticText(self.panel, -1, "This is an example of static text", (100, 10))

        # 指定前景色和背景色的静态文本
        rev = wx.StaticText(self.panel, -1, "Static text reversed Color", (100, 30))
        rev.SetBackgroundColour('black')  # 设置背景颜色
        rev.SetForegroundColour('white')  # 设置前景颜色, 文字的颜色
        rev.SetLabel('set label')

        # 指定居中对齐的的静态文本
        center = wx.StaticText(self.panel, -1, "align center", (100, 50), (140, -1), wx.ALIGN_RIGHT)
        center.SetForegroundColour('white')
        center.SetBackgroundColour('black')

        # 指定字体的静态文本
        str = "You can also change the font."
        text = wx.StaticText(self.panel, -1, str, (20, 100))
        font = wx.Font(18, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        text.SetFont(font)

        # 显示多行文本
        multiLine = wx.StaticText(self.panel, -1, "Your text\ncan be split\n", (20, 150), style=wx.ALIGN_RIGHT)
        multiLine.SetForegroundColour('red')

        userName = wx.StaticText(self.panel, -1, "UserName:", (20, 180))
        userNameField = wx.TextCtrl(self.panel, -1, "Enter your name", (100, 180))
        userNameField.SetInsertionPoint(0)
        pwd = wx.StaticText(self.panel, -1, "Password:", (20, 210))
        pwdField = wx.TextCtrl(self.panel, -1, "", (100, 210), style=wx.TE_PASSWORD | wx.TE_RIGHT)
        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
        sizer.AddMany([userName, userNameField, pwd, pwdField])
        self.panel.SetSizer(sizer)


class SliderFrame(wx.Frame):
    '''滑块实例代码'''
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Slider Example', size=(500, 400))
        self.panel = wx.Panel(self, -1)
        self.count = 0
        # self.createSlider()
        # self.createSpinCtrl()
        self.createProgress()

    def createProgress(self):
        '''创建一个进度条'''
        self.progress = wx.Gauge(self.panel, -1, 100, (20, 50), (250, 25))
        self.progress.SetBezelFace(10)
        self.progress.SetShadowWidth(10)
        self.progress.SetForegroundColour('red')
        self.progress.SetBackgroundColour('white')
        self.Bind(wx.EVT_IDLE, self.onIdle)

    def onIdle(self, e):
        '''控制进度条的填充程度'''
        self.count = self.count + 1
        if self.count >= 100:
            self.count = 0
        self.progress.SetValue(self.count)

    def createSpinCtrl(self):
        '''创建微调显示控件'''
        sc = wx.SpinCtrl(self.panel, -1, 'abcdefg', (10, 30), (80, -1))
        sc.SetRange(1, 100)
        sc.SetValue(10)

    def createSlider(self):
        '''创建滑块'''
        self.count = 0
        self.slider = wx.Slider(self.panel, -1, 50, 1, 1000,
                                pos=(10, 10),
                                size=(250, -1),
                                style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS)
        self.slider.SetTickFreq(50)
        self.Bind(wx.EVT_SLIDER, self.sliderChanged, self.slider)

    def sliderChanged(self, event):
        '''滑块发生变化的事件'''
        print(self.slider.GetValue())


class CheckboxFrame(wx.Frame):
    """复选框部分的的示例代码"""
    def __init__(self):
        """初始化方法"""
        wx.Frame.__init__(self, None, -1, 'CheckBox Example', size=(400, 300))
        self.panel = wx.Panel(self, -1)
        # self.createCheckbox()
        # self.radioButton()
        # self.createListbox()
        # self.createPulldownBox()
        self.createTextPulldownBox()

    def createCheckbox(self):
        """创建一组复选框"""
        wx.CheckBox(self.panel, -1, 'walden', pos=(35, 40))
        wx.CheckBox(self.panel, -1, 'healer', pos=(35, 60))
        wx.CheckBox(self.panel, -1, 'xiaoming', pos=(35, 80))

    def radioButton(self):
        """创建一组单选框"""
        radio1 = wx.RadioButton(self.panel, -1, 'radio1', pos = (20, 20), style=wx.RB_GROUP)
        radio2 = wx.RadioButton(self.panel, -1, 'radio2', pos=(20, 40))
        radio3 = wx.RadioButton(self.panel, -1, 'radio3', pos=(20, 60), style=wx.RB_GROUP)
        radio4 = wx.RadioButton(self.panel, -1, 'radio4', pos=(20, 80))

        list = ["one", "two", "three", "four", "five", "six", "seven", "wight", "night"]
        radioBox = wx.RadioBox(self.panel, -1, "A Radio Box", pos=(100, 10), size=wx.DefaultSize, choices=list, majorDimension=3)
        radioBox.ShowItem(3, False)

    def createListbox(self):
        """创建一个列表选择框"""
        list = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen']
        listBox = wx.ListBox(self.panel, -1, (20, 20), (80, 120), list, wx.LB_SINGLE)
        listBox.SetSelection(3)

    def createPulldownBox(self):
        """创建一个下拉选择框"""
        list = ["one", "two", "three", "four", "five", "six", "seven", "wight", "night"]
        wx.Choice(self.panel, -1, (20, 20), choices=list)

    def createTextPulldownBox(self):
        """带文本框的下拉选择"""
        list = ["one", "two", "three", "four", "five", "six", "seven", "wight", "night"]
        wx.ComboBox(self.panel, -1, 'default Value', (15, 30), wx.DefaultSize, list, wx.CB_SIMPLE)




def createCheckboxApp():
    """创建一个复选框"""
    app = wx.App()
    frame = CheckboxFrame()
    frame.Show()
    app.MainLoop()


def createApp():
    app = wx.App()
    frame = TextFrame()
    frame.Show(True)
    app.MainLoop()


def createSliderExample():
    app = wx.App()
    frame = SliderFrame()
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    # createApp()
    # createSliderExample()
    createCheckboxApp()